//
//  MTLiveFunctionView.m
//  MeToo
//
//  Created by 俊松何 on 2018/7/18.
//  Copyright © 2018年 tianxie. All rights reserved.
//

#import "MTLiveFunctionView.h"

@implementation MTLiveFunctionModel

-(instancetype)initWithName:(NSString *)name image:(NSString *)image type:(MTLiveFunction)type
{
    if(self = [super init])
    {
        _image = image;
        _name = name;
        _type = type;
    }
    return self;
}

@end

@interface MTLiveFunctionItemCell()
@property(nonatomic,strong) UIImageView *iconImageView;
@property(nonatomic,strong) UILabel *nameLable;


@end

@implementation  MTLiveFunctionItemCell


-(instancetype)initWithFrame:(CGRect)frame
{
    if(self =[super initWithFrame:frame])
    {
        [self makeUI];
    }
    return self;
}
-(void)makeUI
{
    _iconImageView = [UIImageView new];
    _iconImageView.userInteractionEnabled = YES;
    _nameLable = [UILabel new];
    [self addSubview:_iconImageView];
    [self addSubview:_nameLable];
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX).offset(0);
        make.top.mas_equalTo(self.mas_top).offset(20);
        make.width.mas_equalTo(@(30));
        make.height.mas_equalTo(@(30));
    }];
    [_nameLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX).offset(0);
         make.top.mas_equalTo(self.iconImageView.mas_bottom).offset(10);
    }];
    _nameLable.textColor = [UIColor whiteColor];
    _nameLable.font = [UIFont systemFontOfSize:14];
    self.backgroundColor = [UIColor clearColor];
}

-(void)setModel:(MTLiveFunctionModel *)model
{
    _model = model;
    self.iconImageView.image = [UIImage imageNamed:model.image];
    self.nameLable.text = model.name;
}

@end


@interface MTLiveFunctionView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic)  UICollectionView *collectionView;
@property(nonatomic,weak) KLCPopup *pop;

@property(nonatomic,strong) NSMutableArray *functionArray;

@property(nonatomic,assign) BOOL isBeautiful;


@end

@implementation MTLiveFunctionView

-(NSMutableArray *)functionArray
{
    if(_functionArray == nil)
    {
        MTLiveFunctionModel *model1 = [[MTLiveFunctionModel alloc] initWithName:@"美颜" image:@"live_more_beauty_on" type:MTLiveFunctionBeautiful];
        MTLiveFunctionModel *model2 = [[MTLiveFunctionModel alloc] initWithName:@"翻转" image:@"live_more_flipcamera" type:MTLiveFunctionFlip];
        MTLiveFunctionModel *model3 = [[MTLiveFunctionModel alloc] initWithName:@"闪光灯" image:@"live_more_flashlight_off" type:MTLiveFunctionFlash];
        MTLiveFunctionModel *model4 = [[MTLiveFunctionModel alloc] initWithName:@"直播主题" image:@"live_more_notice" type:MTLiveFunctionNotice];
        MTLiveFunctionModel *model5 = [[MTLiveFunctionModel alloc] initWithName:@"静音" image:@"live_more_vol_on" type:MTLiveFunctionMute];
        MTLiveFunctionModel *model6 = [[MTLiveFunctionModel alloc] initWithName:@"管理" image:@"live_more_administrator" type:MTLiveFunctionManager];
//       MTLiveFunctionModel *model6 = [[MTLiveFunctionModel alloc] initWithName:@"帮助" image:@"live_more_help" type:MTLiveFunctionNone];
//       MTLiveFunctionModel *model7 = [[MTLiveFunctionModel alloc] initWithName:@"分享" image:@"live_more_share" type:MTLiveFunctionShare];
        _functionArray = [NSMutableArray arrayWithObjects:model1,model2,model3,model4,model5,model6,nil];
    }
   return  _functionArray;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if(self =[super initWithFrame:frame])
    {
        [self makeUI];
        _isBeautiful = YES;
    }
    return self;
}

-(void)makeUI
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [_collectionView registerClass:[MTLiveFunctionItemCell class] forCellWithReuseIdentifier:@"MTLiveFunctionItemCellID"];
    [self addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top).offset(0);
         make.right.mas_equalTo(self.mas_right).offset(0);
         make.left.mas_equalTo(self.mas_left).offset(0);
         make.bottom.mas_equalTo(self.mas_bottom).offset(0);
    }];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.functionArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MTLiveFunctionItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MTLiveFunctionItemCellID" forIndexPath:indexPath];
    cell.model = self.functionArray[indexPath.row];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MTLiveFunctionModel *model = self.functionArray[indexPath.row];
    switch (model.type) {
        case MTLiveFunctionBeautiful:
            self.isBeautiful = !_isBeautiful;
            if(self.isBeautiful)
            {
                model.image = @"live_more_beauty_on";
            }else
            {
                model.image = @"live_more_beauty_off";
            }
            [self.collectionView reloadData];
            if([self.delegate respondsToSelector:@selector(mtLiveFunctionViewBeautifulClick:)])
            {
                [self.delegate mtLiveFunctionViewBeautifulClick:self.isBeautiful];
            }
            break;
        case MTLiveFunctionFlash:
            if([self.delegate respondsToSelector:@selector(mtLiveFunctionViewFlashClick:)])
            {
              BOOL switchFlash =  [self.delegate mtLiveFunctionViewFlashClick:YES];
                if(switchFlash)
                {
                     model.image = @"live_more_flashlight_on";
                }else
                {
                    model.image = @"live_more_flashlight_off";
                }
                [self.collectionView reloadData];
            }
            break;
        case MTLiveFunctionFlip:
        {
            if([self.delegate respondsToSelector:@selector(mtLiveFunctionViewFlipClick)])
            {
                [self.delegate mtLiveFunctionViewFlipClick];
            }
            
        }break;
        case MTLiveFunctionNotice:
        {
            if([self.delegate respondsToSelector:@selector(mtLiveFunctionViewNoticeClick)])
            {
                [self.delegate mtLiveFunctionViewNoticeClick];
            }
            
        }break;
        case MTLiveFunctionShare:
        {
            if([self.delegate respondsToSelector:@selector(mtLiveFunctionViewShareClick)])
            {
                [self.delegate mtLiveFunctionViewShareClick];
            }
            
        }break;
        case MTLiveFunctionMute:
        {
            if([self.delegate respondsToSelector:@selector(mtLiveFunctionViewMute:)])
            {
               BOOL off =  [self.delegate mtLiveFunctionViewMute:YES];
               if(off)
               {
                    model.image = @"live_more_vol_off";
               }else{
                    model.image = @"live_more_vol_on";
               }
                [self.collectionView reloadData];
            }
        }break;
        case  MTLiveFunctionManager:
        {
            if([self.delegate respondsToSelector:@selector(mtLiveFunctionViewManagerClick)])
            {
                [self.delegate mtLiveFunctionViewManagerClick];
            }
            [self dismis];
        }
        case MTLiveFunctionNone: break;
        default:
            break;
    }
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((WIDTH - 1) / 3, 80);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

-(void)show
{
    KLCPopup    *pop = [KLCPopup popupWithContentView:self];
    pop.showType = KLCPopupShowTypeSlideInFromBottom;
    pop.dismissType = KLCPopupDismissTypeSlideOutToBottom;
    pop.maskType = KLCPopupMaskTypeClear;
    self.pop = pop;
    [pop showAtCenter:CGPointMake(WIDTH/2.0, HEIGHT - self.frame.size.height/2) inView:[[UIApplication sharedApplication].delegate window]];
}

-(void)dismis
{
    [self.pop dismiss:YES];
}

@end
